package com.ease.core.components {
   import com.ease.core.events.DockingEvent;
   
   import flash.display.DisplayObject;
   import flash.events.MouseEvent;
   
   import mx.controls.Button;
   import mx.controls.Image;
   import mx.controls.tabBarClasses.Tab;
   import mx.core.DragSource;
   import mx.events.ItemClickEvent;
   import mx.managers.DragManager;

	public class ViewTab extends Tab {
		public static const EVENT_DOCK:String="dock";
		public static const FORMAT:String="ViewTab";
		public static const CLOSE_BUTTON_VISIBLE_SELECTED:String="close_button_visible_selected";
		public static const CLOSE_BUTTON_VISIBLE_ROLLOVER:String="close_button_visible_rollover";

		private var _closeButtonVisiblePolicy:String = CLOSE_BUTTON_VISIBLE_SELECTED;
		/* rollover state */
		private var _rollingOver:Boolean=false;

		private var closeButton:Button;
      
		public function ViewTab() {
			super();

			mouseChildren=true; // We need to enabled mouseChildren so our closeButton can receive mouse events.
			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

			setStyle("fillColors", [0xFFFFFF, 0xF0F0F0]);
			setStyle("fillAlphas", [1, 1]);
			setStyle("borderStyle", "solid");
//			setStyle("cornerRadius", 8);

			setStyle("borderThickness", 1);
		}

		override protected function createChildren():void {
			super.createChildren();

			if (!closeButton) {
				closeButton=new Button();
				closeButton.name="closeButton"

				closeButton.mouseEnabled=true;
				closeButton.focusEnabled=false;

				closeButton.width=closeButton.height=10;
				closeButton.styleName="closeButton";

				closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
				addChild(closeButton);
			}
		}
      
		override protected function measure():void {
			super.measure();
			if (selected) {
            maxWidth = measuredWidth += closeButton.width;
			}
         else {
            maxWidth = measuredWidth;
         }
         
         maxWidth = 200;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			// We need to make sure that the closeButton and the indicator are
			// above all other display items for this button. Otherwise the button
			// skin or icon or text are placed over the closeButton and indicator.
			// That's no good because then we can't get clicks and it looks funky.
			setChildIndex(closeButton, numChildren - 1);
			//this.visible = false;
			closeButton.visible = false;
			closeButton.includeInLayout = false;

			if (this.selected) {
				closeButton.visible = true;
				closeButton.enabled = true;
			}
			else {
				if (this._rollingOver && this._closeButtonVisiblePolicy == ViewTab.CLOSE_BUTTON_VISIBLE_ROLLOVER) {
					closeButton.visible = true;
					closeButton.enabled = true;
					closeButton.includeInLayout = true;
				}
				else {
					closeButton.visible = false;
					closeButton.enabled = false;
				}
			}

			if (closeButton.visible) {
				var gap:int=(unscaledHeight - closeButton.height) / 2;
				closeButton.x=unscaledWidth - closeButton.width - gap;
				closeButton.y=gap;
			}
		}

		/**
		 * A string representing when to show the close button for the tab.
		 * Possible values include: SuperTab.CLOSE_ALWAYS, SuperTab.CLOSE_SELECTED,
		 * SuperTab.CLOSE_ROLLOVER, SuperTab.CLOSE_NEVER
		 */
		public function get closeButtonVisiblePolicy():String {
			return _closeButtonVisiblePolicy;
		}

		public function set closeButtonVisiblePolicy(value:String):void {
			this._closeButtonVisiblePolicy=value;
			this.invalidateDisplayList();
		}

		/*
		 * We keep track of the rolled over state internally so we can set the
		 * closeButton to enabled or disabled depending on the state.
		 */
		override protected function rollOverHandler(event:MouseEvent):void {
			this._rollingOver=true;
			super.rollOverHandler(event);
		}

		override protected function rollOutHandler(event:MouseEvent):void {
			this._rollingOver=false;
			super.rollOutHandler(event);
		}

		private function onCloseButtonClick(event:MouseEvent):void {
			event.stopPropagation();
			event.preventDefault();

			if (event.target is Button && "closeButton" == event.target.name) {
				var itemCloseEvent:ItemClickEvent = new ItemClickEvent(DockingEvent.CLOSED, true, true);
				itemCloseEvent.index = parent.getChildIndex(this);
				closeButton.removeEventListener(MouseEvent.CLICK, onCloseButtonClick);
				dispatchEvent(itemCloseEvent);
			}
		}

		[Embed(source="/assets/icon/viewDragImageClass.gif")]
		public var dragImageClass:Class;

		private function onMouseMove(event:MouseEvent):void {
			if (event.buttonDown == true && this.getStageWindow().docking) {
				var ds:DragSource=new DragSource();
				ds.addData(this, ViewTab.FORMAT);

				var dragImage:Image = new Image();
				dragImage.source=dragImageClass;
				dragImage.height=16;
				dragImage.width=16;

				DragManager.doDrag(Button(event.currentTarget), ds, event, dragImage, 10 - this.mouseX, 10 - this.mouseY, 0.8);
			}
		}

		private function onMouseUp(event:MouseEvent):void {
			this.getStageWindow().docking=false;
		}

		private function onMouseDown(event:MouseEvent):void {
			this.getStageWindow().docking=true;
		}

		public function getStageWindow():StageWindow {
			var object:DisplayObject=this;
			while (!(object is StageWindow)) {
				object=object.parent;
			}
			return StageWindow(object);
		}

		public function getViewWindow():ViewWindow {
			var object:DisplayObject=this;
			while (!(object is ViewWindow)) {
				object=object.parent;
			}
			return ViewWindow(object);
		}
	}
}